package Limit.Game.MarkSix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

/**
 * 購買彩票時數據保存到臨時表的相關程式
 * 方法：
 * loadCurrentData 			加載當前玩家臨時彩票資料庫裡的彩票信息
 * saveMarkTicketsOfTemp	保存當前玩家抽取的彩票信息到臨時資料庫
 * countMarkTickets			統計當前玩家是否有未完成的彩票信息
 * resetMarkTicketsNum		重設彩票號碼及注碼
 * delMarkTicketsNum		刪除當前玩家彩票臨時庫裡的彩票信息
 * buyMarkTicketsNum		將當前玩家的彩票信息從臨時資料庫保存到正式彩票資料庫
 *　@author LiHuan 2013.4.2
 */
public class MarkSix_Tickets_Table_Temp {

	protected int charid;
	protected int type;
	protected int wager = 0;
	protected int ticketsNum = 0;
	protected int num = 0;
	public String[] results = new String[8];

	public void setCharid(int charid)			{this.charid = charid;	}
	public void setType(int type)				{this.type = type;	}
	public void setWager(int wager)				{this.wager = wager;	}
	public void setTicketsNum(int ticketsNum)	{this.ticketsNum = ticketsNum;	}
	public void setNumber(int num)				{this.num = num;}
	
	public void loadCurrentData(){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM game_marksix_tickets_temp WHERE char_id=?");
			pstm.setInt(1, this.charid);
			rs = pstm.executeQuery();
			
			if (rs.next()) 
			{
				this.results[0] = String.valueOf(rs.getInt("Wager"));
				this.results[7] = String.valueOf(rs.getInt("type"));
				for (int i = 1; i < 7; i++){
					this.results[i] = rs.getString("num"+ i);
				}
			}
		} 
		catch (SQLException e) {		} 
		finally {SQLUtil.close(rs);SQLUtil.close(pstm);	SQLUtil.close(con);}
	}
	public void saveMarkTicketsOfTemp(){
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			if (this.countMarkTickets() > 0 && this.num > 0){
				pstm = con.prepareStatement("UPDATE game_marksix_tickets_temp SET num" + this.num + "=? WHERE char_id=?");
				pstm.setInt(1, this.ticketsNum);
				pstm.setInt(2, this.charid);
			} else if (this.countMarkTickets() > 0 && this.wager > 0){
				pstm = con.prepareStatement("UPDATE game_marksix_tickets_temp SET wager=? WHERE char_id=?");
				pstm.setInt(1, this.wager);
				pstm.setInt(2, this.charid);
			} else {
				pstm = con.prepareStatement("INSERT INTO game_marksix_tickets_temp SET Type=?, Wager=?, char_id=?");
				pstm.setInt(1, this.type);
				pstm.setInt(2, this.wager);
				pstm.setInt(3, this.charid);
			}
			pstm.execute();			
		}
		catch (Exception e){System.out.print(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}		
	}
	
	public int countMarkTickets()
	{
		int result = 0;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("SELECT count(*) as cnt FROM game_marksix_tickets_temp WHERE char_id=?");
			pstm.setInt(1, this.charid);
			rs = pstm.executeQuery();
			
			if (rs.next()) 
			{
				result = rs.getInt("cnt");
			}
		} 
		catch (SQLException e) {System.out.print(e.getMessage());} 
		finally {SQLUtil.close(rs);SQLUtil.close(pstm);SQLUtil.close(con);}
		return result;
	}
	
	public void resetMarkTicketsNum(){
		Connection con = null;
		PreparedStatement pstm = null;
		try
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("UPDATE game_marksix_tickets_temp SET Wager=0 ,num1='*' ,num2='*' ,num3='*' ,num4='*' ,num5='*' ,num6='*' WHERE char_id=?");
			pstm.setInt(1, this.charid);
			pstm.execute();
		}
		catch (SQLException e){System.out.println(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}
	}
	
	public void delMarkTicketsNum(){
		Connection con = null;
		PreparedStatement pstm = null;
		try
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("DELETE FROM game_marksix_tickets_temp WHERE char_id=?");
			pstm.setInt(1, this.charid);
			pstm.execute();
		}
		catch (SQLException e){System.out.println(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}		
	}
	
	public void buyMarkTicketsNum(String[] data){
		Connection con = null;
		PreparedStatement pstm = null;
		try
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO game_marksix_tickets SET char_id=? ,type=? ,wager=? ,num1=? ,num2=? ,num3=? ,num4=? ,num5=? ,num6=? ,datetime=SYSDATE()");
			pstm.setInt(1,this.charid);
			pstm.setInt(2,this.type);
			pstm.setInt(3,this.wager);
			pstm.setString(4,data[0]);
			pstm.setString(5,data[1]);
			pstm.setString(6,data[2]);
			pstm.setString(7,data[3]);
			pstm.setString(8,data[4]);
			pstm.setString(9,data[5]);
			pstm.execute();

		}
		catch (SQLException e){System.out.println(e.getMessage());}
		finally {SQLUtil.close(pstm);SQLUtil.close(con);}	
	}
}
